
package ch.hearc.coursJava.IO.persistence.properties;

import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import ch.hearc.coursJava.GUI.frame.tools.JFrameBase;

public class JFrameProperties extends JFrameBase
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/

	public JFrameProperties()
		{
		addListener();
		setPropriete();
		loadPreferences();
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	private void addListener()
		{
		addWindowListener(new WindowAdapter()
			{

				@Override
				public void windowClosing(WindowEvent arg0)
					{
					System.out.println("Window Closing");
					savePreferences();
					System.out.println("Preferences sauves");
					System.exit(0); // 0 normal, -1 anormal
					}
			});
		}

	private void loadPreferences()
		{
		Properties properties = new Properties();
		try
			{
			FileInputStream fileInputStream = new FileInputStream(PROPERTIES_FILE);
			BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

			properties.load(bufferedInputStream);
			}

		catch (Exception e)
			{
			System.err.println("Preferences file not found");
			}

		int x0 = Integer.valueOf(properties.getProperty("x0", "0"));
		int y0 = Integer.valueOf(properties.getProperty("y0", "0"));
		int width = Integer.valueOf(properties.getProperty("width", "800"));
		int height = Integer.valueOf(properties.getProperty("height", "600"));

		this.setLocation(new Point(x0, y0));
		this.setSize(width, height);
		}

	private void savePreferences()
		{
		Properties properties = new Properties();
		properties.setProperty("x0", "" + this.getLocation().x);
		properties.setProperty("y0", "" + this.getLocation().y);
		properties.setProperty("width", "" + this.getSize().width);
		properties.setProperty("height", "" + this.getSize().height);

		try
			{
			PROPERTIES_FILE.getParentFile().mkdirs();
			FileOutputStream fileOutputStream = new FileOutputStream(PROPERTIES_FILE);
			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

			properties.store(bufferedOutputStream, JFrameProperties.class.getSimpleName());

			bufferedOutputStream.close();
			fileOutputStream.close();
			}
		catch (Exception e)
			{
			System.err.println("Preference non sauve ");
			}
		System.out.println(PROPERTIES_FILE.getName());
		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/
	private static final File PROPERTIES_FILE = new File("config" + File.separator + JFrameProperties.class.getSimpleName());
	}
